home *** CD-ROM | disk | FTP | other *** search
Lisp/Scheme | 1988-04-07 | 8.0 KB | 239 lines | [TEXT/ttxt] |
- ;; Larry Mulcahy 1988
- ;; io
-
- (provide 'io)
- (require 'format-long "long")
- (require 'msg)
- (require 'personality "personal")
- (require 'string)
- (require 'type)
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; display
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-
- (defun display (string &key echo-stream)
- (princ string)
- (and echo-stream (princ string echo-stream)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; read-non-empty-line
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun read-non-empty-line ()
- (prog (result)
- loop (setq result (read-line))
- (if (= (length result) 0)
- (go loop))
- (return result)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; ask
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun ask (&key (type t) echo-stream default prompt quit-tag)
- ; typep from the type module
- (loop
- (let* ((result (prompt-with-default
- :default default
- :type type
- :prompt prompt
- :quit-tag quit-tag
- :echo-stream echo-stream))
- (done (typep result type)))
- (if done
- (return result) ; This is the exit from the loop
- (insist-upon-correct-type type
- :echo-stream echo-stream
- :quittable? quit-tag)))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; get-char
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun get-char () (int-char (get-key)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; y-or-n-p
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun y-or-n-p (&key prompt)
- (if prompt (princ prompt))
- (let ((result (get-one-of-these-characters '(#\y #\Y #\n #\N))))
- (if (member result '(#\y #\Y))
- (progn (princ "Yes") (terpri) t)
- (progn (princ "No") (terpri) nil))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun display-array (arr)
- (for i 0 (1- (length arr)) (msg "[" i "]: " (aref arr i) t)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; format-prompt
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun format-prompt (&key prompt default line-length quit-tag)
- (let ((default-in-a-box (if default
- (format nil " [DEFAULT=~A]" default)
- ""))
- (prompt-string (zap-to-string prompt))
- (quit-string (if quit-tag " or QUIT" "")))
- (format-text-if-its-too-long
- (concatenate 'string prompt-string default-in-a-box quit-string)
- line-length)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; insist-upon-correct-type
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun insist-upon-correct-type (type &key echo-stream quittable?)
- (chastise-careless-users)
- (let ((pretty (format nil "Input should be ~A~A~%"
- (type-pretty-print-string type)
- (if quittable? " or QUIT" ""))))
- (format t pretty)
- (if echo-stream (format echo-stream pretty))))
-
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; chastise-careless-users
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun chastise-careless-users ()
- (let ((message (chastise-careless-users-string)))
- (if (> (length message) 0)
- (format t "~A~%"
- (format-text-if-its-too-long message (line-length))))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; line-length
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun line-length () *line-length*)
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; display-numbered-menu
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun display-numbered-menu (menu &key special echo-stream)
- (let ((s (numbered-list-string menu :special special :indent 2)))
- (format t s)
- (if echo-stream (format echo-stream s))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; prompt-with-default
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun prompt-with-default
- (&key type echo-stream prompt default quit-tag)
- (let* ((entire-prompt (format-prompt :prompt prompt
- :default default
- :line-length (line-length)
- :quit-tag quit-tag))
- (result
- (block outer
- (format t "~A~%" entire-prompt)
- (if echo-stream (format echo-stream "~A~%" entire-prompt))
- (if default
- (if (equal (peek-char) #\newline)
- (progn (read-char)
- (return-from outer default))))
-
- (let ((impurity (read-line)))
- (test-for-quit impurity quit-tag echo-stream)
- (case type
- (string impurity)
- (t (read-from-string impurity)))))))
-
-
- (if echo-stream (format echo-stream "~A~%" result))
- (test-for-quit result quit-tag echo-stream)
- result))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; quit-p
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun quit-p (r)
- (let ((s (zap-to-string r)))
- (if (> (length s) 3)
- (string-equal (string-upcase (subseq s 0 4))
- "QUIT"))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; test-for-quit
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun test-for-quit (input quit-tag echo-stream)
- (if (and quit-tag (quit-p input))
- (progn
- (if echo-stream
- (format echo-stream "QUIT!~%"))
- (throw quit-tag t))))
-
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; get-menu-pick-index
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; returns a zero-origin index to menu
-
- (defun get-menu-pick-index
- (menu &key title echo-stream default special quit-tag)
- (let* ((ul-prompt "Enter the number of one menu entry")
- (menu-big (length menu)))
- (if title
- (progn (format t "~A~%" title)
- (if echo-stream (format echo-stream "~A~%" title))))
- (display-numbered-menu menu
- :echo-stream echo-stream
- :special special)
- (1-
- (ask :prompt ul-prompt
- :type `(integer 1 ,menu-big)
- :echo-stream echo-stream
- :default default
- :quit-tag quit-tag))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; get-menu-pick
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun get-menu-pick
- (menu &key title echo-stream default special quit-tag)
- (let ((index (get-menu-pick-index menu
- :title title
- :echo-stream echo-stream
- :default default
- :special special
- :quit-tag quit-tag)))
- (elt menu index)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; get-one-of-these-characters
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun get-one-of-these-characters (character-bag)
- (let ((done nil)
- (result nil))
- (while (not done)
- (setq result (get-char))
- (write-char result)
- (terpri)
- (if (member result character-bag)
- (setq done t)
- (progn
- (chastise-careless-users)
- (format t "~A~%"
- (format-text-if-its-too-long
- (format nil
- "Please enter one of {~A}"
- (apply #'concatenate
- (cons 'string
- (mapcar #'zap-to-string character-bag))))
- (line-length))))))
- result))
-